/**
 * 
 */
package com.boot.springBoot.util.excel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * Excel  读写工具类 自己练习写的demo
 * @author Administrator
 *
 */
public class Excel {
	
	private static String[] title= {"编号","姓名","性别","年龄"};
	
	/**
	 * 生成Excel 文件
	 */
	private static void excelWrite() {
		
		//创建电子表格
		HSSFWorkbook workbook = new HSSFWorkbook();
		//创建sheet
		HSSFSheet sheet = workbook.createSheet();
		//创建行数据
		HSSFRow row = sheet.createRow(0);
		//创建单元格
		HSSFCell cell = null;
		
		//创建标题
		for (int i = 0; i < title.length; i++) {
			cell = row.createCell(i);
			cell.setCellValue(title[i]);
		}
		//创建数据
		for (int i = 1; i < 15; i++) {
			
			row = sheet.createRow(i);
			
			//第一个单元格
			cell = row.createCell(0);
			cell.setCellValue(i+"");
			//第二个单元格
			cell = row.createCell(1);
			cell.setCellValue("杨某");
			//第三个单元格
			cell = row.createCell(2);
			cell.setCellValue("男");
			//第四个单元格
			cell = row.createCell(3);
			cell.setCellValue("22");
			
		}
		
		//创建一个文件输出的 输出流到workbook
		File file = new File("D:测试包excel.xls");
		try {
			FileOutputStream out = new FileOutputStream(file);
			workbook.write(out);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				workbook.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	/**
	 * 读取Excel 文件

	private static void readerExcel() {
		File file = new File("D:\\测试包\\excel.xls");
		try {
			HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
			HSSFSheet sheet = workbook.getSheetAt(0);
			int firstRow = 0;
			int lastRow = sheet.getLastRowNum();
			for (int i = firstRow; i < lastRow; i++) {
				HSSFRow row = sheet.getRow(i);
				int lastCell = row.getLastCellNum();
				for (int j = 0; j < lastCell; j++) {
					HSSFCell cell = row.getCell(j);
					String value = cell.getStringCellValue();
					System.out.print(value+" ");
				}
				System.out.println();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}*/
	
	
	public static void main(String[] args) {
		System.out.println("----------------准备生成一个Excel---------------");
//		readerExcel();
		System.out.println(System.getProperty("user.dir"));
		
		
	}
}
